package com.jkc.xh.manager;

import android.widget.Toast;

import com.jkc.xh.MainApplication;
import com.jkc.xh.constants.Constant;
import com.jkc.xh.models.StockInfoModel;
import com.jkc.xh.network.ApiClient;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.AddFavStockRequest;
import com.jkc.xh.network.models.AssetInfoResponse;
import com.jkc.xh.network.models.AutoBuyRequest;
import com.jkc.xh.network.models.AutoListResponse;
import com.jkc.xh.network.models.AutoSellRequest;
import com.jkc.xh.network.models.BalListResponse;
import com.jkc.xh.network.models.BussinessListResponse;
import com.jkc.xh.network.models.BuyIndexRequest;
import com.jkc.xh.network.models.DeleteMyFavRequest;
import com.jkc.xh.network.models.EntrustListResponse;
import com.jkc.xh.network.models.FavListResponse;
import com.jkc.xh.network.models.FundsListResponse;
import com.jkc.xh.network.models.HaltSellBuyRequest;
import com.jkc.xh.network.models.HaltSellListResponse;
import com.jkc.xh.network.models.HoldAllResponse;
import com.jkc.xh.network.models.HoldAutoRequest;
import com.jkc.xh.network.models.HoldOneResponse;
import com.jkc.xh.network.models.MaxBuyNumRequest;
import com.jkc.xh.network.models.MaxBuyNumResponse;
import com.jkc.xh.network.models.MayBeanRequest;
import com.jkc.xh.network.models.MayBeanResponse;
import com.jkc.xh.network.models.ResponseEntity;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.network.models.RevokeAutoRequest;
import com.jkc.xh.network.models.RevokeListResponse;
import com.jkc.xh.network.models.RevokeOneRequest;
import com.jkc.xh.network.models.SearchStockResponse;
import com.jkc.xh.network.models.SellMaxCountRequest;
import com.jkc.xh.network.models.TradeSoldAllRequest;
import com.jkc.xh.network.models.TradeSoldPartRequest;
import com.jkc.xh.utils.EventBusUtils;

import retrofit.Call;

/**
 * Created by zhangqingfeng on 2015/12/1.
 */
public class TraderManager {

    /**
     * 2304获得我的委托列表
     *
     * @param page_index
     * @param callBack
     */
    public static Call entrustList(int page_index, final RestCallBack<EntrustListResponse> callBack) {
        final Call<ResponseEntity<EntrustListResponse>> call = ApiClient.getApiService().entrustList(page_index);
        call.enqueue(new RestCallBack<EntrustListResponse>() {
            @Override
            public void success(EntrustListResponse entrustListResponse) {
                EventBusUtils.post(Constant.ACTION_ENTRUST_LIST);
                if (callBack != null) {
                    callBack.success(entrustListResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;

    }

    /**
     * 2201 可撤查询
     *
     * @param page_index
     * @param callBack
     */
    public static Call queryRevokeList(int page_index, final RestCallBack<RevokeListResponse> callBack) {
        final Call<ResponseEntity<RevokeListResponse>> call = ApiClient.getApiService().queryRevokeList(page_index);
        call.enqueue(new RestCallBack<RevokeListResponse>() {
            @Override
            public void success(RevokeListResponse entrustListResponse) {
                EventBusUtils.post(Constant.ACTION_REVOKE_LIST);
                if (callBack != null) {
                    callBack.success(entrustListResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }


    /**
     * 2301 动态总资产
     *
     * @param callBack
     */
    public static Call queryAssetInfo(final RestCallBack<AssetInfoResponse> callBack) {
        final Call<ResponseEntity<AssetInfoResponse>> call = ApiClient.getApiService().queryAssetInfo();
        call.enqueue(new RestCallBack<AssetInfoResponse>() {
            @Override
            public void success(AssetInfoResponse entrustListResponse) {
                EventBusUtils.post(Constant.ACTION_ASSET_INFO);
                if (callBack != null) {
                    callBack.success(entrustListResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * 2307 资金流水
     *
     * @param date_start 开始时间 整数 默认值:0
     * @param date_end   结束时间 整数 默认值:0
     * @param page_index 分页索引，当前页标，从0开始 整数 默认值:0
     * @param callBack
     */
    public static Call queryFundsList(long date_start, long date_end, int page_index, int page_size, final RestCallBack<FundsListResponse> callBack) {
        final Call<ResponseEntity<FundsListResponse>> call = ApiClient.getApiService().queryFundsList(date_start, date_end, page_index, page_size);
        call.enqueue(new RestCallBack<FundsListResponse>() {
            @Override
            public void success(FundsListResponse entrustListResponse) {
                EventBusUtils.post(Constant.ACTION_FUNDS_LIST);
                if (callBack != null) {
                    callBack.success(entrustListResponse);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;

    }

    /**
     * 2308 平仓结算
     *
     * @param callBack
     */
    public static Call queryBalList(int page_index, final RestCallBack<BalListResponse> callBack) {
        final Call<ResponseEntity<BalListResponse>> call = ApiClient.getApiService().queryBalList(page_index);
        call.enqueue(new RestCallBack<BalListResponse>() {
            @Override
            public void success(BalListResponse response) {
                EventBusUtils.post(Constant.ACTION_BAL_LIST);
                if (callBack != null) {
                    callBack.success(response);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }


    /***
     * 2305 当日成交/历史成交
     *
     * @param history    类型，0=当日成交，1=历史成交 整数 枚举:0,1 必填
     * @param page_index
     * @param callBack
     */
    public static Call queryBusinessList(int history, int page_index, final RestCallBack<BussinessListResponse> callBack) {
        final Call<ResponseEntity<BussinessListResponse>> call = ApiClient.getApiService().queryBusinessList(history, page_index);
        call.enqueue(new RestCallBack<BussinessListResponse>() {
            @Override
            public void success(BussinessListResponse response) {
                EventBusUtils.post(Constant.ACTION_BUSSINESS_LIST);
                if (callBack != null) {
                    callBack.success(response);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;

    }

    /**
     * 2202 单笔撤单
     *
     * @param entrust_id
     */
    public static Call revokeOneRequest(String entrust_id, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().revokeOneRequest(new RevokeOneRequest(entrust_id));
        call.enqueue(new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                EventBusUtils.post(Constant.ACTION_REVOKE_ONE);
                if (callBack != null) {
                    callBack.success(o);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * 2203 批量撤单
     */
    public static Call revokeAllRequest(final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().revokeAllRequest();
        call.enqueue(new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                EventBusUtils.post(Constant.ACTION_REVOKE_ALL);
                if (callBack != null) {
                    callBack.success(o);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });

        return call;
    }

    /**
     * 2501 分笔持仓
     *
     * @param page_index
     * @param callBack
     */
    public static Call queryHoldOneList(int page_index,int issell, final RestCallBack<HoldOneResponse> callBack) {
        final Call<ResponseEntity<HoldOneResponse>> call = ApiClient.getApiService().queryHoldOneListRequest(page_index, 1,issell);
        call.enqueue(new RestCallBack<HoldOneResponse>() {
            @Override
            public void success(HoldOneResponse holdOneModel) {
                EventBusUtils.post(Constant.ACTION_HOLD_ONE);
                if (callBack != null) {
                    callBack.success(holdOneModel);
                }

            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }


    /**
     * 2502 合并持仓
     *
     * @param callBack
     */
    public static Call queryHoldAllList(int page_index,int issell, final RestCallBack<HoldAllResponse> callBack) {
        final Call<ResponseEntity<HoldAllResponse>> call = ApiClient.getApiService().queryHoldAllListRequest(page_index,issell);
        call.enqueue(new RestCallBack<HoldAllResponse>() {
            @Override
            public void success(HoldAllResponse response) {
                EventBusUtils.post(Constant.ACTION_HOLD_ALL);
                if (callBack != null) {
                    callBack.success(response);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * 2002 行情查询
     *
     * @param code
     * @param callBack
     */
    public static Call queryStockInfoRequest(String code, final RestCallBack<StockInfoModel> callBack) {
        final Call<ResponseEntity<StockInfoModel>> call = ApiClient.getApiService().queryStockInfoRequest(code);
        call.enqueue(new RestCallBack<StockInfoModel>() {
            @Override
            public void success(StockInfoModel stockInfoModel) {
                if (callBack != null) {
                    callBack.success(stockInfoModel);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * 2003 最大可买数量
     *
     * @param price
     * @param callBack
     */
    public static Call queryMaxBuyNum(String price, String code, int amount, final RestCallBack<MaxBuyNumResponse> callBack) {
        final Call<ResponseEntity<MaxBuyNumResponse>> call = ApiClient.getApiService().queryMaxBuyNum(new MaxBuyNumRequest(price, code, amount));
        call.enqueue(new RestCallBack<MaxBuyNumResponse>() {
            @Override
            public void success(MaxBuyNumResponse response) {
                if (callBack != null) {
                    callBack.success(response);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * 2004 大约可低抵用券
     *
     * @param amount
     * @param price
     * @param callBack
     */
    public static Call queryMayBean(String price, long amount, String code, final RestCallBack<MayBeanResponse> callBack) {
        final Call<ResponseEntity<MayBeanResponse>> call = ApiClient.getApiService().queryMayBean(new MayBeanRequest(price, amount, code));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2005 股票检索
     *
     * @param key      关键字 1-6位数字
     * @param callBack
     * @return
     */
    public static Call searchStock(String key, final RestCallBack<SearchStockResponse> callBack) {
        final Call<ResponseEntity<SearchStockResponse>> call = ApiClient.getApiService().searchStockRequest(key);
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2401 添加自选股
     *
     * @param stockCode 股票代码
     * @param callBack
     * @return
     */
    public static Call addFavStock(String stockCode, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().addFavStockRequest(new AddFavStockRequest(stockCode));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2402 删除自选股
     *
     * @param id
     * @param callBack
     */
    public static Call deleteMyFacStock(String id, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().deleteMyFavStock(new DeleteMyFavRequest(id));
        call.enqueue(callBack);
        return call;
    }

    /***
     * 2403 我的自选股
     *
     * @param page_index
     * @param callBack
     */
    public static Call queryFavList(int page_index, final RestCallBack<FavListResponse> callBack) {
        final Call<ResponseEntity<FavListResponse>> call = ApiClient.getApiService().queryFavListRequest(page_index);
        call.enqueue(new RestCallBack<FavListResponse>() {
            @Override
            public void success(FavListResponse response) {
                if (callBack != null) {
                    callBack.success(response);
                }
            }

            @Override
            public void failure(RestError error) {
                Toast.makeText(MainApplication.getInstance(), error.msg, Toast.LENGTH_SHORT).show();
                if (callBack != null) {
                    callBack.failure(error);
                }
            }
        });
        return call;
    }

    /**
     * @param code
     * @param price
     * @param amount
     * @param buytype
     * @param is_pay_bean
     */
    public static Call buyStock(String code, String price, String amount, int buytype, int is_pay_bean, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().buyIndex(new BuyIndexRequest(code, price, amount, buytype, is_pay_bean));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2101 分笔卖出
     *
     * @param request
     * @param callBack
     */
    public static Call soldPartStock(TradeSoldPartRequest request, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().soldPartRequest(request);
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2102 分笔卖出时最大可卖数量
     *
     * @param code
     * @param hid
     * @param callBack
     */
    public static Call querySellPartMaxCount(String code, String hid, final RestCallBack<MaxBuyNumResponse> callBack) {
        final Call<ResponseEntity<MaxBuyNumResponse>> call = ApiClient.getApiService().querySellPartMaxCount(new SellMaxCountRequest(code, hid));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2103 合并卖出
     *
     * @param request
     * @param callBack
     */
    public static Call soldAllStock(TradeSoldAllRequest request, final RestCallBack<Object> callBack) {
        final Call<ResponseEntity<Object>> call = ApiClient.getApiService().soldAllRequest(request);
        call.enqueue(callBack);
        return call;
    }

    /**
     * 2104 合并卖出时最大可卖数量
     *
     * @param code
     * @param callBack
     */
    public static Call querySellAllMaxCount(String code, final RestCallBack<MaxBuyNumResponse> callBack) {
        final Call<ResponseEntity<MaxBuyNumResponse>> call = ApiClient.getApiService().querySellAllMaxCount(new SellMaxCountRequest(code));
        call.enqueue(callBack);

        return call;
    }

    public static Call autoDeposit(String hid, int ischecked, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().autoDeposit(new HoldAutoRequest(hid, ischecked));
        call.enqueue(callBack);
        return call;
    }

    public static Call autoDelay(String hid, int ischecked, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().autoDelay(new HoldAutoRequest(hid, ischecked));
        call.enqueue(callBack);
        return call;
    }

    public static Call haltDpt(String hid, int ischecked, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().haltDpt(new HoldAutoRequest(hid, ischecked));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 自动买入
     *
     * @param code
     * @param upprice
     * @param downprice
     * @param amount
     * @param callBack
     * @return
     */
    public static Call autoBuy(String code, String upprice, String downprice, long amount, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().autoBuyRequest(new AutoBuyRequest(code, upprice, downprice, amount));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 自动卖出
     *
     * @param code
     * @param upprice
     * @param downprice
     * @param amount
     * @param hid
     * @param callBack
     * @return
     */
    public static Call autoSell(String code, String upprice, String downprice, long amount, String hid, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().autoSellRequest(new AutoSellRequest(code, upprice, downprice, amount, hid));
        call.enqueue(callBack);
        return call;
    }

    /**
     * 返回策略列表
     *
     * @param page_index
     * @param callBack
     * @return
     */
    public static Call getAutoList(int page_index, final RestCallBack<AutoListResponse> callBack) {
        Call<ResponseEntity<AutoListResponse>> call = ApiClient.getApiService().queryAutoListRequest(page_index);
        call.enqueue(callBack);
        return call;
    }

    public static Call revokeAuto(String auto_id, final RestCallBack<Object> callBack) {
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().revokeAutoRequest(new RevokeAutoRequest(auto_id));
        call.enqueue(callBack);
        return call;
    }

    public static Call getHaltSellList(int page_index, final RestCallBack<HaltSellListResponse> callBack) {
        Call<ResponseEntity<HaltSellListResponse>> call = ApiClient.getApiService().queryHaltsellRequest(page_index);
        call.enqueue(callBack);
        return call;
    }

    public static Call doHaltStockBuy(String haltsell_id,long amount, final RestCallBack<Object> callBack){
        Call<ResponseEntity<Object>> call = ApiClient.getApiService().haltsellBuyRequest(new HaltSellBuyRequest(haltsell_id,amount));
        call.enqueue(callBack);
        return call;
    }

}
